package com.cloudlead.fleetAction.service.impl;

import com.cloudlead.commonData.service.BaseDoorService;
import com.cloudlead.commonData.service.RegionService;
import com.cloudlead.commonData.vo.BaseDoor;
import com.cloudlead.commonData.vo.Region;
import com.cloudlead.fleetAction.service.BaseDoorEditSer;
import com.cloudlead.fleetService.vo.BaseDoorVo;
import com.cloudlead.fleetService.vo.RegionVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by wyj on 2015/8/18.
 */
@Service
public class BaseDoorEditSerImpl implements BaseDoorEditSer{

    @Autowired
    BaseDoorService baseDoorService;
    @Autowired
    RegionService regionService;
    @Override
    public List<BaseDoorVo> findAllDoor(BaseDoorVo vo) {
        BaseDoor bd=new BaseDoor();
        BeanUtils.copyProperties(vo,bd);//类型转换
        if(vo.getBase_place()!=null) {
            Region rv = new Region();
            BeanUtils.copyProperties(vo.getBase_place(), rv);//类型转换
        }

        List<BaseDoor> lbd=baseDoorService.list(bd,null);
        List<BaseDoorVo>  lbdvo= new ArrayList<BaseDoorVo>();
        for(Iterator<BaseDoor> it = lbd.iterator();it.hasNext();) {//遍历
            BaseDoorVo bdv=new BaseDoorVo();
            BaseDoor bdr=it.next();
            BeanUtils.copyProperties(bdr, bdv);//类型转换
            RegionVo rv =new RegionVo();
            bdv.setBase_place(rv);
            lbdvo.add(bdv);
        }
        return lbdvo;
    }
    public String deleteDoor(String id){
        BaseDoor br= baseDoorService.view(id);
        if(br!=null){
            baseDoorService.delete(id);
        }else
            return "error ID";
        return "success";
    }
    public String createDoor(BaseDoorVo vo){
        BaseDoor bv=new BaseDoor();
        BeanUtils.copyProperties(vo, bv);
        if(vo.getBasePlaceId()!=null&&!vo.getBasePlaceId().equals("")){
            Region rv =regionService.view(vo.getBasePlaceId());
            bv.setBasePlace(rv);
        }
        /*if(vo.getBaseProvinceId()!=null&&!vo.getBaseProvinceId().equals("")){
            if(vo.getBasePlaceId()==null||vo.getBasePlaceId().equals("")){
                Region rv1 = regionService.view(vo.getBaseProvinceId());
                bv.setBasePlace(rv1);
            }
        }*/

        baseDoorService.create(bv);
        return "success";
    }
    public String editBaseDoor(BaseDoorVo vo){
        BaseDoor bv=new BaseDoor();
        BeanUtils.copyProperties(vo, bv);
        if(vo.getBasePlaceId()!=null&&!vo.getBasePlaceId().equals("")){
            Region rv =regionService.view(vo.getBasePlaceId());
            bv.setBasePlace(rv);
        }
        /*if(vo.getBaseProvinceId()!=null&&!vo.getBaseProvinceId().equals("")){
            if(vo.getBasePlaceId()==null||vo.getBasePlaceId().equals("")){
                Region rv1 = regionService.view(vo.getBaseProvinceId());
                bv.setBasePlace(rv1);
            }
        }*/
        baseDoorService.update(bv);
        return "success";
    }
}
